

--[[ Globals --]]
GuildXpBarUI = {}

--[[ Main addon frame creation and main local vars ]]--

function GuildXpBarUI.CreateXPBar()
	local abBar = CreateFrame("Frame", "GuildXpBarBar", UIParent)
	abBar:SetMovable(true)
	abBar:EnableMouse(true)
	abBar:SetSize(116, 26)

	abBar:SetScript("OnMouseDown", function (self, button)
						if button == "LeftButton" and not GuildXpBar.GetConfigValue("locked") then
 							self:StartMoving();
 						end
	end)
	abBar:SetScript("OnMouseUp", function (self, button)
						if button == "LeftButton" then
 							self:StopMovingOrSizing();
 						end
	end)
	abBar:SetPoint("CENTER", 0, 80)
	abBar:Hide()

	abBarBackground = abBar:CreateTexture("GuildXpBarBarBackground", "BACKGROUND")
	abBarBackground:SetSize(GuildXpBar.GetConfigValue("barWidth"), 13)
	--abBarBackground:SetTexture(0, 0, 0, .5)
	abBarBackground:SetTexture(0, 0, 0,GuildXpBar.GetConfigValue("barAlpha")) -- how trasparent it is 0.0-1.0
	abBarBackground:SetPoint("TOP", 0, -2)

	local abBarBorder = abBar:CreateTexture("GuildXpBarBarBorder", "OVERLAY")
	abBarBorder:SetSize(GuildXpBar.GetConfigValue("barWidth"), 20)
	--abBarBorder:SetTexture("Interface\\Tooltips\\UI-StatusBar-Border")
	abBarBorder:SetPoint("TOP", 0, 2)

	local abBarText = abBar:CreateFontString("GuildXpBarText", "OVERLAY", "GameFontHighlight")
	abBarText:SetSize(110, 17)
	abBarText:SetPoint("TOP", 0, 0)

	local abBarStatus = CreateFrame("StatusBar", "GuildXpBarStatusBar", abBar)
	abBarStatus:SetSize(GuildXpBar.GetConfigValue("barWidth"), 13)
	abBarStatus:SetPoint("TOP", 0, -2)
	--print("status bar is at level: " .. abBarStatus:GetFrameLevel())
	--abBarStatus:SetFrameLevel(1)
	abBarStatus:SetFrameLevel(abBarStatus:GetParent():GetFrameLevel())

	local abBarStatusTexture = abBarStatus:CreateTexture("GuildXpBarStatusBarTexture", "BACKGROUND")
	abBarStatusTexture:SetTexture("Interface\\TargetingFrame\\UI-StatusBar")

	abBarStatus:SetStatusBarTexture(abBarStatusTexture)
	--abBarStatus:SetFrameLevel(1)
	--abBarStatus:SetStatusBarColor(1, 1, 0)
	--print("status bar is at level: " .. abBarStatus:GetFrameLevel())
	--abBarStatus:SetFrameLevel(1)
	--print("status bar is at level: " .. abBarStatus:GetFrameLevel())
	
	
	CreateFrame( "GameTooltip", "MyScanningTooltip", nil, "GameTooltipTemplate" ); -- Tooltip name cannot be nil
	MyScanningTooltip:SetOwner( WorldFrame, "ANCHOR_NONE" );
-- Allow tooltip SetX() methods to dynamically add new lines based on these
	MyScanningTooltip:AddFontStrings(
    MyScanningTooltip:CreateFontString( "$parentTextLeft1", nil, "GameTooltipText" ),
    MyScanningTooltip:CreateFontString( "$parentTextRight1", nil, "GameTooltipText" ) );


end

--[[ Config UI --]]
function GuildXpBarUI.CreateConfigMenu()
	local configPanel = CreateFrame("Frame", "GuildXpBarConfigFrame", UIParent)
	configPanel.name = "GuildXpBar"
	configPanel.okay = function (self) return end
	configPanel.cancel = function (self) return end

	local addonName, addonTitle, addonNotes = GetAddOnInfo('GuildXpBar')
	local configPanelText = configPanel:CreateFontString(nil, 'ARTWORK', 'GameFontNormalLarge')
	configPanelText:SetPoint('TOPLEFT', 16, -16)
	configPanelText:SetText(addonTitle)

	local configPanelDesc = configPanel:CreateFontString(nil, 'ARTWORK', 'GameFontHighlightSmall')
	--configPanelDesc:SetHeight(32)
	configPanelDesc:SetPoint('TOPLEFT', configPanelText, 'BOTTOMLEFT', 0, -8)
	configPanelDesc:SetPoint('RIGHT', configPanel, -32, 0)
	configPanelDesc:SetNonSpaceWrap(true)
	configPanelDesc:SetJustifyH('LEFT')
	configPanelDesc:SetJustifyV('TOP')
	configPanelDesc:SetText(addonNotes)

	local showOptionDesc = configPanel:CreateFontString(nil, 'ARTWORK', 'GameFontNormal')
	showOptionDesc:SetPoint('TOPLEFT', configPanelDesc, 'BOTTOMLEFT', -2, -10)
	showOptionDesc:SetText("What to show")

	local showAlways = GuildXpBarUI.CreateCheckButton("Show Daily Guild XP instead of current level", configPanel,"allow you to change the percent that is show to the daily amount or the percent of the current guild level", "showAlways", 'InterfaceOptionsCheckButtonTemplate')
	showAlways:SetPoint('TOPLEFT', showOptionDesc, 'BOTTOMLEFT', 0, 0)

	--local showCombat = GuildXpBarUI.CreateCheckButton("In Combat", configPanel,"Show while in combat", "showCombat", 'InterfaceOptionsCheckButtonTemplate')
	--showCombat:SetPoint('TOPLEFT', showAlways, 'BOTTOMLEFT', 0, 0)
	--GuildXpBarUI.SetupInverseDependentControl(showAlways, showCombat)

	local barAlphaDesc = configPanel:CreateFontString(nil, 'ARTWORK', 'GameFontNormal')
	barAlphaDesc:SetPoint('TOPLEFT', showAlways, 'BOTTOMLEFT', -10, -10)
	barAlphaDesc:SetText("Bar Alpha (Opacity): ")

	local barAlphaSlider = CreateFrame("Slider", configPanel:GetName() .. "BarAlphaSlider", configPanel, "OptionsSliderTemplate")
	barAlphaSlider:SetMinMaxValues(0.0, 1.0)
	barAlphaSlider:SetValueStep(0.01)
	barAlphaSlider:SetValue(GuildXpBar.GetConfigValue("barAlpha"))
	_G[barAlphaSlider:GetName() .. "High"]:SetText("1.0")
	_G[barAlphaSlider:GetName() .. "Low"]:SetText("0.0")
	barAlphaSlider:SetScript("OnValueChanged", GuildXpBarUI.BarAlphaSliderChanged)
	barAlphaSlider:SetPoint('TOPLEFT', barAlphaDesc, 'BOTTOMLEFT', 10, 0)

	local barScaleDesc = configPanel:CreateFontString(nil, 'ARTWORK', 'GameFontNormal')
	barScaleDesc:SetPoint('TOPLEFT', barAlphaSlider, 'BOTTOMLEFT', -10, -10)
	barScaleDesc:SetText("Bar Scale: ")

	local barScaleSlider = CreateFrame("Slider", configPanel:GetName() .. "barScaleSlider", configPanel, "OptionsSliderTemplate")
	barScaleSlider:SetMinMaxValues(.25, 3.0)
	barScaleSlider:SetValueStep(0.01)
	barScaleSlider:SetValue(GuildXpBar.GetConfigValue("barScale"))
	_G[barScaleSlider:GetName() .. "High"]:SetText("3.0")
	_G[barScaleSlider:GetName() .. "Low"]:SetText("0.25")
	barScaleSlider:SetScript("OnValueChanged", GuildXpBarUI.BarScaleSliderChanged)
	barScaleSlider:SetPoint('TOPLEFT', barScaleDesc, 'BOTTOMLEFT', 10, 0)
--[[	
	local barWidthDesc = configPanel:CreateFontString(nil, 'ARTWORK', 'GameFontNormal')
	barWidthDesc:SetPoint('TOPLEFT', barScaleSlider, 'BOTTOMLEFT', -10, -10)
	barWidthDesc:SetText("Bar Width: ")

	local barWidthSlider = CreateFrame("Slider", configPanel:GetName() .. "barWidthSlider", configPanel, "OptionsSliderTemplate")
	barWidthSlider:SetMinMaxValues(.25, 3.0)
	barWidthSlider:SetValueStep(0.01)
	barWidthSlider:SetValue(GuildXpBar.GetConfigValue("barWidth"))
	_G[barWidthSlider:GetName() .. "High"]:SetText("300")
	_G[barWidthSlider:GetName() .. "Low"]:SetText("50")
	barWidthSlider:SetScript("OnValueChanged", GuildXpBarUI.BarWidthSliderChanged)
	barWidthSlider:SetPoint('TOPLEFT', barWidthDesc, 'BOTTOMLEFT', 10, 0)
]]---
	InterfaceOptions_AddCategory(configPanel)
end

function GuildXpBarUI.BarTextEditBoxChanged(self, isUserInput)
	if isUserInput then
		GuildXpBar.SetConfigValue("barText", self:GetText())
		GuildXpBar.UpdateBar()
	end
end

function GuildXpBarUI.BarAlphaSliderChanged(self, value)
	GuildXpBar.SetConfigValue("barAlpha", value)
	--GuildXpBarBar:SetAlpha(value)
	--print("value = " ..value)
	abBarBackground:SetTexture(0, 0, 0, value)
end

function GuildXpBarUI.BarScaleSliderChanged(self, value)
	GuildXpBar.SetConfigValue("barScale", value)
	GuildXpBarBar:SetScale(value)
end

--function GuildXpBarUI.BarWidthSliderChanged(self, value)
--	GuildXpBar.SetConfigValue("barWidth", value)
--	
--	abBarBackground:SetSize(value, 13)
--end

function GuildXpBarUI.CreateCheckButton(name, parent, tooltipText, configKey, template)
	local button = CreateFrame('CheckButton', parent:GetName() .. name, parent, template)
	_G[button:GetName() .. 'Text']:SetText(name)
	button:SetChecked(GuildXpBar.GetConfigValue(configKey))
	button:SetScript('OnClick', function(self)
		if self:GetChecked() then
			GuildXpBar.SetConfigValue(configKey, true)
		else 
			GuildXpBar.SetConfigValue(configKey, false)
		end
		if ( self.dependentControls ) then
			if ( self:GetChecked() ) then
				for _, control in pairs(self.dependentControls) do
					control:Enable()
				end
			else
				for _, control in pairs(self.dependentControls) do
					control:Disable()
				end
			end
		end
		if ( self.inverseDependentControls ) then
			if ( self:GetChecked() ) then
				for _, control in pairs(self.inverseDependentControls) do
					control:Disable()
				end
			else
				for _, control in pairs(self.inverseDependentControls) do
					control:Enable()
				end
			end
		end
		GuildXpBar.ShowOrHideBar()

	end)
	button:SetScript('OnEnter', function(self)
		GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
		GameTooltip:SetText(tooltipText, nil, nil, nil, 1, 1)
		GameTooltip:Show() end)
	button:SetScript('OnLeave', function(self) GameTooltip:Hide() end)

	return button
end

function GuildXpBarUI.SetupDependentControl (dependency, control)
	if ( not dependency ) then
		return
	end
	
	assert(control)
	
	dependency.dependentControls = dependency.dependentControls or {}
	tinsert(dependency.dependentControls, control)

	if ( control.type ~= CONTROLTYPE_DROPDOWN ) then
		control.Disable = function (self) getmetatable(self).__index.Disable(self) _G[self:GetName().."Text"]:SetTextColor(GRAY_FONT_COLOR.r, GRAY_FONT_COLOR.g, GRAY_FONT_COLOR.b) end
		control.Enable = function (self) getmetatable(self).__index.Enable(self) _G[self:GetName().."Text"]:SetTextColor(HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b) end
		if dependency:GetChecked() then
			control:Enable()
		else
			control:Disable()
		end
	else
		control.Disable = function (self) UIDropDownMenu_DisableDropDown(self) end
		control.Enable = function (self) UIDropDownMenu_EnableDropDown(self) end
	end

end

function GuildXpBarUI.SetupInverseDependentControl (dependency, control)
	if ( not dependency ) then
		return
	end
	
	assert(control)
	
	dependency.inverseDependentControls = dependency.inverseDependentControls or {}
	tinsert(dependency.inverseDependentControls, control)

	if ( control.type ~= CONTROLTYPE_DROPDOWN ) then
		control.Disable = function (self) getmetatable(self).__index.Disable(self) _G[self:GetName().."Text"]:SetTextColor(GRAY_FONT_COLOR.r, GRAY_FONT_COLOR.g, GRAY_FONT_COLOR.b) end
		control.Enable = function (self) getmetatable(self).__index.Enable(self) _G[self:GetName().."Text"]:SetTextColor(HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b) end
		if dependency:GetChecked() then
			control:Disable()
		else
			control:Enable()
		end
	else
		control.Disable = function (self) UIDropDownMenu_DisableDropDown(self) end
		control.Enable = function (self) UIDropDownMenu_EnableDropDown(self) end
	end

end